package com.sankuai.inf.leaf.server.service;

//import com.alibaba.druid.pool.DruidDataSource;
//import com.sankuai.inf.leaf.common.PropertyFactory;

import com.sankuai.inf.leaf.segment.dao.UserDao;
import com.sankuai.inf.leaf.segment.dao.impl.UserDaoImpl;
import com.sankuai.inf.leaf.segment.model.User;
import com.sankuai.inf.leaf.server.CommonException;
import com.sankuai.inf.leaf.server.util.MD5;
import com.sankuai.inf.leaf.server.util.RandomHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.List;

/**
 * @author final
 * @description
 * @date 2019-09-09 21:47
 */
@Slf4j
@Service
public class UserService {

    UserDao userDao;

    //    DruidDataSource dataSource;
    @Autowired
    DataSource dataSource;

    public UserService() throws SQLException {
//        Properties properties = PropertyFactory.getProperties();
//        // Config dataSource
//        dataSource = new DruidDataSource();
//        dataSource.setUrl(properties.getProperty(Constants.LEAF_JDBC_URL));
//        dataSource.setUsername(properties.getProperty(Constants.LEAF_JDBC_USERNAME));
//        dataSource.setPassword(properties.getProperty(Constants.LEAF_JDBC_PASSWORD));
//        dataSource.init();
//        // Config Dao
//        userDao = new UserDaoImpl(dataSource);
    }

    @PostConstruct
    private void init(){
        userDao = new UserDaoImpl(dataSource);
    }

    public User getUserByUserName(String username) {
        return userDao.getUserByUserName(username);
    }

    public void addUser(String username, String password) throws Exception {
        //判断用户是否已存在
        User userByUserName = userDao.getUserByUserName(username);
        if (!ObjectUtils.isEmpty(userByUserName)) {
            //throw new CommonException(100501, "用户已存在");
            throw new Exception("用户已存在");

        }
        try {
            //加密密码
            String salt = RandomHelper.getRandomString(10);
            String hash = MD5.authHash(password, salt);
            User user = new User();
            user.setUsername(username);
            user.setPassword(hash);
            user.setSalt(salt);
            //保存用户
            userDao.insertUser(user);
        } catch (Exception ex) {
            log.error("addUser error:{}",ex);
            throw new Exception("用户创建失败");
        }
    }


    public List<User> listUser() {
        return userDao.listUser();
    }

    public void updateUser(User user) throws Exception {
        if(ObjectUtils.isEmpty(user)){
            throw new CommonException(100505, "用户名或密码不能为空");
        }
        //判断用户是否已存在
        User userByUserName = userDao.getUserByUserName(user.getUsername());
        if (ObjectUtils.isEmpty(userByUserName)) {
            throw new CommonException(100506, "用户不存在");
        }
        try {
            String salt = RandomHelper.getRandomString(10);
            String hash = MD5.authHash(user.getPassword(), salt);
            userByUserName.setPassword(hash);
            userByUserName.setSalt(salt);
            userDao.updateUser(userByUserName);
        }catch (Exception ex){
            log.error("updateUser error:{}",ex);
            throw new CommonException(100507, "用户密码修改失败");
        }

    }
}
